package com.cheng.controller;


import com.cheng.pojo.User;
import com.cheng.pojo.Users;
import com.cheng.service.UserService;
import com.cheng.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    //contorller调sevice层
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    //contorller调sevice层
    @Autowired
    @Qualifier("UsersServiceImpl")
    private UsersService usersService;

    //管理员登录
    @RequestMapping("/selectUser")
    public String selectUser(String userCode, String userPassword, Model model,HttpSession session){
        User user = userService.selectUser(userCode, userPassword);
        session.setAttribute("user",user);

        if (user ==null){
            model.addAttribute("error","用户名或密码错误！");
            return "login";

        }

        return "frame";
    }

    //管理员退出
    @RequestMapping("/userloginOut")
    public String userloginOut(HttpSession session){
        session.removeAttribute("user");
        return "login";

    }

    //普通用户登录
    @RequestMapping("/selectUsers")
    public String selectUsers(HttpSession session,String name, String password, Model model){
        Users users = usersService.selectUsers(name, password);
        session.setAttribute("users",users);

        if (users==null){
            model.addAttribute("error","用户名密码错误！");
            return "userLogin";
        }

        return "home page" ;

    }

    //注册时查看用户名是否存在
    @ResponseBody
    @GetMapping(value = "/a3",produces = "text/html; charset=UTF-8")
    public String a3(String name){

        String msg="";
        if (name!=null){
            Users users = usersService.select_information(name);
            if (users==null){
                msg="ok";
            }else {
                msg="no";
            }
        }
        return msg;
    }

    //注册时查看商家名是否存在
    @ResponseBody
    @GetMapping(value = "/a4",produces = "text/html; charset=UTF-8")
    public String a4(String username){

        String msg="";
        if (username!=null){
            User user = userService.select_information(username);
            if (user==null){
                msg="ok";
            }else {
                msg="no";
            }
        }
        return msg;
    }

    //退出
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.removeAttribute("usersLoginInfo");
        return "userLogin";

    }

    //注册用户
    @RequestMapping("/addUser")
    public String addUser(Users users){
        usersService.addUsers(users);
        return "userLogin";
    }

    //修改密码
    @RequestMapping("/updatePassword")
    public String updatePassword(Users users,Model model){
        int i = usersService.updatePassword(users);
        if (i==0){
            model.addAttribute("error","用户名或邮箱错误！");
            return "updatepassword";
        }
        return "userLogin";

    }

    //查询商家信息
    @RequestMapping("/select_information")
    public String select_information(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        User user1 = userService.select_information(user.getUserCode());
        model.addAttribute("information",user1);



        return "update_information";
    }

    //修改商家信息
    @RequestMapping("/update_information")
    public String update_information(String userName,String userPassword,String phone,String address,HttpSession session){
        User user = (User) session.getAttribute("user");
        String userCode = user.getUserCode();
        userService.update_information(userName,userPassword,phone,address,userCode);

        return "login";
    }

    //查询用户信息
    @RequestMapping("/select_users")
    public String update_users(HttpSession session,Model model){
        Users users= (Users) session.getAttribute("users");
        Users users1 = usersService.select_information(users.getName());
        model.addAttribute("information",users1);



        return "front_end/travel_updateusers";
    }

    //修改用户信息
    @RequestMapping("/update_users")
    public String update_users(String userEmail,String userPassword,HttpSession session){
        Users users= (Users) session.getAttribute("users");
        usersService.update_information(userEmail,userPassword,users.getName());

        return "userLogin";
    }

    //注册商家用户
    @RequestMapping("/adduser")
    public String adduser(User user){
        System.out.println("11111111111111111111"+user);
        userService.addUser(user);
        return "login";

    }

}
